{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[参考：Quicksort in Python](https://stackabuse.com/quicksort-in-python/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 4, 5, 6, 8]\n"
     ]
    }
   ],
   "source": [
    "def partition(array, start, end):\n",
    "    pivot = array[start]\n",
    "    small = start + 1\n",
    "    large = end\n",
    "    \n",
    "    while True:\n",
    "        while small <= large and array[large] >= pivot:\n",
    "            large = large - 1\n",
    "            \n",
    "        while small <= large and array[small] <= pivot:\n",
    "            small = small + 1\n",
    "            \n",
    "        if small <= large:\n",
    "            temp = array[small]\n",
    "            array[small] = array[large]\n",
    "            array[large] = temp\n",
    "        else:\n",
    "            break\n",
    "        \n",
    "    array[start], array[large] =  array[large], array[start]\n",
    "    return large\n",
    "        \n",
    "    \n",
    "def quick_sort(array, start, end):\n",
    "    if start >= end:\n",
    "        return\n",
    "    \n",
    "    p = partition(array, start, end)\n",
    "    quick_sort(array, start, p - 1)\n",
    "    quick_sort(array, p + 1, end)   \n",
    "        \n",
    "tempArray = [8, 5, 5, 4, 1, 6]\n",
    "quick_sort(tempArray, 0, len(tempArray) - 1)\n",
    "print(tempArray)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
